
import org.hibernate.Session;
import org.hibernate.Transaction;

public class WriteToDateBase implements IWriter
{
    private static Session session;
    private static TCryptoFile crypto_file;
    public void OpenWriter()
    {
        session = HibernateUtil.currentSession();
    }
    public void CloseWriter()
    {
        HibernateUtil.closeSession();
    }
    public void WriteFile(TFile file)
    {        
        Transaction tx = session.beginTransaction();
        //TFile file = new TFile(content, type, language);
        session.save(file);
        session.flush();
        tx.commit();
    }
    public void WriteArchiveFile(TFile file, String compressor_name, long archive_file_size, int ratio, long time, String compression_level)
    {
        Transaction tx = session.beginTransaction();
        TArchiveFile arch_file = new TArchiveFile(compressor_name, archive_file_size, 
                                                  ratio, time, compression_level, file);    
        session.save(arch_file);
        session.flush();
        tx.commit();
    }
    public void WriteCryptoFile(TFile file, String crypto_algorithm, long crypto_file_size, String mode)
    {
        Transaction tx = session.beginTransaction();
        crypto_file = new TCryptoFile(crypto_algorithm, crypto_file_size, mode, file);     
        session.save(crypto_file);
        session.flush();
        tx.commit();
    }
    public TCryptoFile getCryptoFile()
    {
        return crypto_file;
    }
    public void WriteArchiveCryptoFile(String compressor_name, long archive_crypto_file_size, int ratio, long time)
    {
        Transaction tx = session.beginTransaction();
        TCryptoFile f = getCryptoFile();
        TArchiveCryptoFile archive_crypto_file = new TArchiveCryptoFile(compressor_name, 
                                                                        archive_crypto_file_size, 
                                                                        ratio, time, f);     
        session.save(archive_crypto_file);
        session.flush();
        tx.commit();
    }
}
